<template>
  <div class="login">
    <div class="logo">
      <img src="../static/images/login_logo.png" alt="" />
    </div>

    <div class="form_container">
      <van-form @submit="onSubmit">
        <van-field
          class="login_field"
          v-model="user_tel"
          name="user_tel"
          placeholder="手机号"
          :rules="[
            { required: true, message: '请填写手机号' },
            { validator, message: '请输入正确手机号' },
          ]"
        />
        <van-field
          class="login_field"
          v-model="password"
          type="password"
          name="password"
          placeholder="密码"
          :rules="[
            { required: true, message: '请填写密码' },
            { validator, message: '密码请输入6-18位' },
          ]"
        />
        <div class="enter_button">
          <img src="../static/images/enter_button.png" alt="" />
          <van-button round block type="info" native-type="submit"
            >提交
          </van-button>
        </div>
      </van-form>
    </div>

    <div class="foot">
      <div class="agree">快捷登录</div>
      <div class="register_click" @click="register">我要注册</div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      user_tel: "",
      password: "",
      show: false,
    };
  },
  methods: {
    ...mapMutations(["changeLogin"]),
    // 校验函数返回 true 表示校验通过，false 表示不通过
    validator(val) {
      return /1\d{10}/.test(val) || /^[0-9A-Za-z]{6,20}$/.test(val);
    },
    onSubmit(values) {
      this.request.post("/login", values).then((res) => {
        if (res.status !== 0) {
          // return alert("登录失败!")
          return this.$toast.fail("密码错误!");
        }
        this.$router.push({ path: "/main" });
        this.changeLogin({ Authorization: res.token });
      });
    },
    register() {
      this.$router.push({ path: "/register" });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 750rpx;
  height: 100%;
  margin: 0 auto;

  .logo {
    padding: 16.7% 0 8.6%;
    width: 69.3%;
    height: 13.2%;
    margin: 0 auto;

    img {
      width: 100%;
      height: 100%;
    }
  }
  .form_container {
    width: 65.2%;
    height: 27.3%;
    margin: 0 auto;

    .enter_button {
      box-sizing: border-box;
      position: relative;
      padding: 8% 0 0 0;
      width: 22.4%;
      height: 24.7%;
      margin: 0 auto;
      display: block;

      button {
        background: none;
        border: none;
        border-radius: 50%;
        font-size: 0;
      }
      img {
        position: absolute;
        width: 100%;
        top: 24.7%;
        left: 0;
      }
    }
  }

  .form_container .login_field,
  .form_container .register_field:not(.security_code) {
    box-sizing: border-box;
    width: 100%;
    height: 18.5%;
    // padding: 6% 0 0 8%;
    margin-bottom: 14.9%;
    background-color: rgb(244, 244, 244);
    border-radius: 59px;
  }

  .van-field__body {
    height: 37.3%;
    align-items: baseline;
  }

  .van-field__control::-webkit-input-placeholder {
    color: rgb(144, 144, 144);
    font-family: "Adobe Heiti Std";
  }

  .foot {
    position: fixed;
    width: 70.9%;
    padding: 0 14%;
    bottom: 7.5%;
    font-family: "SourceHanSansCN";
    font-weight: 600;
    font-size: 0.5em;
  }

  .foot div {
    display: inline-block;
  }

  .foot .login_click {
    float: right;
  }

  .foot .register_click {
    float: right;
  }
}
</style>